package listeners;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JTextPane;


import view.Block;
import view.DrawPanel;
import view.PictureModel;

public class DraggedMouseMotionListener extends MouseMotionAdapter{
	private SelectMouseListener selectListener;
	private PictureModel pictureModel;
	private DrawPanel paint;
	private JTextPane textPane;
	public Point selectPoint;
	public DraggedMouseMotionListener(SelectMouseListener selectListener,DrawPanel paint,PictureModel pictureModel,JTextPane textPane){
		this.selectListener = selectListener;
		this.paint = paint;
		this.pictureModel = pictureModel;
		this.textPane = textPane;
	}
	@Override
	public void mouseDragged(MouseEvent event) {
		if(!selectListener.isSelect){
			return;
		}
		Block block = selectListener.select;
		
		if(block.haveLine(pictureModel)){
			textPane.setText("You can`t move blocks with lines!!!");
			return;
		}
		//clean image
		block.cleanDraw(paint.getGraphics2D());
		paint.repaint();
		//calculate new control point of block
		Point oldPressPoint = selectListener.selectPoint;
		Point newPressPoint = event.getPoint();
		int deltaX = newPressPoint.x - oldPressPoint.x;
		int deltaY = newPressPoint.y - oldPressPoint.y;
		Point oldPoint = block.getPoint();
		Point newPoint = new Point(oldPoint.x + deltaX, oldPoint.y + deltaY);
		block.setPoint(newPoint);
		selectListener.selectPoint = newPressPoint;
		//repaint all
		pictureModel.draw();
	}
}
